package pe.edu.upc.studio.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import pe.edu.upc.studio.excepcion.DAOExcepcion;
import pe.edu.upc.studio.modelo.Articulo;
import pe.edu.upc.studio.util.ConexionBD;

public class ArticuloDAO extends BaseDAO {

	public void insertarArticulo(Articulo objArticulo) throws DAOExcepcion {
		System.out.println("RegistroArticuloDAO: insertar(Articulo vo)");
		String query = "INSERT INTO Articulo(Tipo,Marca,Modelo,Caracteristicas,CostoxHora,idCategoria) VALUES (?,?,?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, objArticulo.getTipo());
			stmt.setString(2, objArticulo.getMarca());
			stmt.setString(3, objArticulo.getModelo());
			stmt.setString(4, objArticulo.getCaracteristica());
			stmt.setDouble(5, objArticulo.getCostoxHora());
			stmt.setInt(6, objArticulo.getCategoria().getIdCategoria());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

	public void insertarServicio(Articulo objArticulo) throws DAOExcepcion {
		System.out.println("RegistroArticuloDAO: insertar(Articulo vo)");
		String query = "INSERT INTO Articulo(Caracteristicas,CostoxHora,idCategoria) VALUES (?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, objArticulo.getCaracteristica());
			stmt.setDouble(2, objArticulo.getCostoxHora());
			stmt.setInt(3, objArticulo.getCategoria().getIdCategoria());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

}
